<?php
// Contiene metodos y funciones para gestionar la base de datos
include_once("DataBaseManager.php");
include 'ChromePhp.php';
class FrontendManager extends DataBaseManager{

	// atributos heredados
	// $db
	
	// constructor
	function __construct(){
		//$serverName, $port, $db_name, $username, $password
		$serverName = "(local)";
		$port = "1433";
		$db_name = "pruebas";
		$username = "usuario";
		$password = "123";
		parent::__construct($serverName, $port, $db_name, $username, $password);
	}

	//>>******************************************************************************************************<<//
	//>>********** METODOS PARA JORNADA 1: DE 18:00 HRS DEL DIA ANTERIOR A 06:00 DEL DIA ACTUAL **************<<//
	//>>******************************************************************************************************<<//

	//metodo para obtener los datos de la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getVelPromedioJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener las unidades producidas en la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getUnidadesProducidasJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select top 1 tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K 
		from tabla_TC15KM1A where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'
		order by tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K desc";
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener el numero de paros en la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getNumeroDeParosJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select ISNULL(SUM(CAST(tabla_TC15KM1A.MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener el Tiempo Muerto en Segundos en la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getTiempoMuertoSegJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select ISNULL(SUM(CAST(MAQ_1ATC15K_TIEMPO_MUERTO_SEG_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}


	//metodo para obtener el Tiempo Muerto en Minutos en la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getTiempoMuertoMinJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select ISNULL(SUM(CAST(MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}


	//metodo para obtener Las unidades de Desperdicio en la jornada anterior de 18:00 hrs del dia anterior a las 06:00 hrs del dia actual
	function getUnidadesDesperdicioJornada1(){
		$fechaAnterior = date('Y-m-d',strtotime("-1 days"));
		$fechaAnterior = $fechaAnterior . " 18:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 06:00:00.000";

		$sql="select top 1 tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K 
		from tabla_TC15KM1A where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'
		order by tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K desc";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}


	function getGraficaVelocidadJornada1($LineaMaquinaTubo, $jornada){
		$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}

		$arreglo;
		//18:00 a 19:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 07:00:00.000";
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//19:00 a 20:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 07:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 08:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//20:00 a 21:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 09:00:00.000";
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//21:00 a 22:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 10:00:00.000";
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//22:00 a 23:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 11:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//23:00 a 23:59 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:59:59.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 12:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//00:00 a 01:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 00:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 01:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 13:00:00.000";
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//01:00 a 02:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 01:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 02:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 14:00:00.000";	
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//02:00 a 03:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 02:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 03:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 15:00:00.000";
		 }
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//03:00 a 04:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 03:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 04:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 16:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//04:00 a 05:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 04:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 05:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 17:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//05:00 a 06:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 18:00:00.000"; 
		}
		
		$sql="select ISNULL(AVG(CAST(p." . $campo . " as int)),0)
		from " . $tabla . " p where p.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		return $arreglo;
	}

	function getLabelsJornada1(){
		$labels = '"18:00-19:00","19:00-20:00","20:00-21:00","21:00-22:00","22:00-23:00","23:00-00:00","00:00-01:00",
		"01:00-02:00", "02:00-03:00","03:00-04:00","04:00-05:00","05:00-06:00"';
		return $labels;

	}
	//>>******************************************************************************************************<<//
	//>>********** METODOS PARA JORNADA 1: DE 18:00 HRS DEL DIA ANTERIOR A 06:00 DEL DIA ACTUAL **************<<//
	//>>******************************************************************************************************<<//

	//metodo para obtener los datos de la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getVelPromedioJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener las unidades producidas en la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getUnidadesProducidasJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select top 1 tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K 
		from tabla_TC15KM1A where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'
		order by tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K desc";
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener el numero de paros en la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getNumeroDeParosJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select ISNULL(SUM(CAST(tabla_TC15KM1A.MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;
	}

	//metodo para obtener el Tiempo muerto en Segundos en la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getTiempoMuertoSegJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select ISNULL(SUM(CAST(MAQ_1ATC15K_TIEMPO_MUERTO_SEG_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}

	//metodo para obtener el Tiempo muerto en Minutos en la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getTiempoMuertoMinJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select ISNULL(SUM(CAST(MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}

	//metodo para obtener Las unidades de Desperdicio en la jornada anterior de 06:00 hrs del dia actual a las 18:00 hrs del dia actual
	function getUnidadesDesperdicioJornada2(){
		$fechaAnterior = date('Y-m-d');
		$fechaAnterior = $fechaAnterior . " 06:00:00.000";

		$fechaActual = date('Y-m-d');
		$fechaActual = $fechaActual . " 18:00:00.000";

		$sql="select top 1 tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K 
		from tabla_TC15KM1A where tabla_TC15KM1A.DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'
		order by tabla_TC15KM1A.MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K desc";				
		
		try{
			$result = $this->db->Execute($sql);
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
		}
		return $result;		
	}

	function getGraficaVelocidadJornada2(){

		$arreglo;
		//06:00 a 07:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 07:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//07:00 a 08:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 07:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 08:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//08:00 a 09:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 09:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//09:00 a 10:00 del dia anterior
		$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 10:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//10:00 a 11:00 del dia anterior
		$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 11:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//11:00 a 12:00 del dia anterior
		$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 12:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//12:00 a 13:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 13:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//13:00 a 14:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 14:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//14:00 a 15:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 15:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//15:00 a 16:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 16:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//16:00 a 17:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 17:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//17:00 a 18:00 del dia actual
		$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
		$limiteSuperior = date('Y-m-d') . " 18:00:00.000"; 
		
		$sql="select ISNULL(AVG(CAST(tabla_TC15KM1A.MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A	where tabla_TC15KM1A.DateTime between '" . $limiteInferior ."' AND '". $limiteSuperior . "'";
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		return $arreglo;
	}

	function getLabelsJornada2(){
		$labels = '"06:00-07:00","07:00-08:00","08:00-09:00","09:00-10:00","10:00-11:00","11:00-12:00","12:00-13:00",
		"13:00-14:00", "14:00-15:00","15:00-16:00","16:00-17:00","17:00-18:00"';
		return $labels;

	}

	function getEstadisticasTiempoReal($fecha1, $fecha2, $LineaMaquinaTubo){
	//ChromePhp::log("TANGANANI1: " . $fecha1);
	//ChromePhp::log("TANGANANI2: " . $fecha2);

	$tabla = "";
	$campo1 = "";
	$campo2 = "";
	$campo3 = "";
	$campo4 = "";
	$campo5 = "";
	$campo6 = "";

	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo1 = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";
		$campo2 = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";
		$campo3 = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";
		$campo4 = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";
		$campo5 = "MAQ_1ATC15K_TIEMPO_MUERTO_SEG_M1ATC15K";
		$campo6 = "MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo1 = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";
		$campo2 = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";
		$campo3 = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";
		$campo4 = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";
		$campo5 = "MAQ_1BTC15K_TIEMPO_MUERTO_SEG_M1BTC15K";
		$campo6 = "MAQ_1BTC15K_TIEMPO_MUERTO_MIN_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo1 = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";
		$campo2 = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";
		$campo3 = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";
		$campo4 = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";
		$campo5 = "MAQ_2ATC15K_TIEMPO_MUERTO_SEG_M2ATC15K";
		$campo6 = "MAQ_2ATC15K_TIEMPO_MUERTO_MIN_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo1 = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";
		$campo2 = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";
		$campo3 = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";
		$campo4 = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";
		$campo5 = "MAQ_2BTC15K_TIEMPO_MUERTO_SEG_M2BTC15K";
		$campo6 = "MAQ_2BTC15K_TIEMPO_MUERTO_MIN_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo1 = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";
		$campo2 = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";
		$campo3 = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";
		$campo4 = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";
		$campo5 = "MAQ_3ATC15K_TIEMPO_MUERTO_SEG_M3ATC15K";
		$campo6 = "MAQ_3ATC15K_TIEMPO_MUERTO_MIN_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo1 = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";
		$campo2 = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";
		$campo3 = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";
		$campo4 = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";
		$campo5 = "MAQ_3BTC15K_TIEMPO_MUERTO_SEG_M3BTC15K";
		$campo6 = "MAQ_3BTC15K_TIEMPO_MUERTO_MIN_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo1 = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";
		$campo2 = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";
		$campo3 = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";
		$campo4 = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";
		$campo5 = "MAQ_4ATC15K_TIEMPO_MUERTO_SEG_M4ATC15K";
		$campo6 = "MAQ_4ATC15K_TIEMPO_MUERTO_MIN_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo1 = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";
		$campo2 = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";
		$campo3 = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";
		$campo4 = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";
		$campo5 = "MAQ_4BTC15K_TIEMPO_MUERTO_SEG_M4BTC15K";
		$campo6 = "MAQ_4BTC15K_TIEMPO_MUERTO_MIN_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo1 = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";
		$campo2 = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";
		$campo3 = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";
		$campo4 = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";
		$campo5 = "MAQ_5ATC15K_TIEMPO_MUERTO_SEG_M5ATC15K";
		$campo6 = "MAQ_5ATC15K_TIEMPO_MUERTO_MIN_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo1 = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
		$campo2 = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
		$campo3 = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
		$campo4 = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
		$campo5 = "MAQ_5BTC15K_TIEMPO_MUERTO_SEG_M5BTC15K";
		$campo6 = "MAQ_5BTC15K_TIEMPO_MUERTO_MIN_M5BTC15K";
	}else {
		return null;
	}



		$sql="select ISNULL(AVG(CAST(p4." . $campo1 . " as int)),0) as velPromedio,

(select top 1 cast(p1." . $campo2 ." as int) from " . $tabla . " p1
where p1.DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'
order by p1.DateTime desc ) as UnidadesProducidas,

(select top 1 cast(p2." . $campo3 . " as int)
from " . $tabla . " p2 where p2.DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'
order by p2.DateTime desc) as UnidadesDesperdicio,

(select top 1 ISNULL(CAST(p3." . $campo4 . " as int),0) 
from " . $tabla . " p3 where p3.DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'
order by p3.DateTime desc ) as NumeroParos,
ISNULL(SUM(CAST(" . $campo5 . " as int)),0) as TMuertoSeg,
ISNULL(SUM(CAST(" . $campo6 . " as int)),0) as TMuertoMin

from " . $tabla . " p4 where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'";
//ChromePhp::log("TANGANANI: " . $sql);
try{
	$result = $this->db->Execute($sql);
}
catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
	throw new Exception($ex->getMessage());
}
return $result;

}	


//retorna estadisticas de la maquina 1 para el año especificado
function getEstadisticaAnio($anio)
{


	$fechaAnterior = date('Y-m-d');
	$fechaAnterior = $fechaAnterior . " 06:00:00.000";

	$fechaActual = date('Y-m-d');
	$fechaActual = $fechaActual . " 18:00:00.000";

		//$sql="select ISNULL(SUM(CAST(MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K as decimal(18,3))),0)
		//from tabla_TC15KM1A where DateTime between '" . $fechaAnterior ."' AND '". $fechaActual . "'";				

	$sql="
	select top 1(
		select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
		from tabla_TC15KM1A where datepart(month,DateTime)=1 and datepart(year,DateTime)=" . $anio . " ) as enero,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=2 and datepart(year,DateTime)=" . $anio . " ) as febrero,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=3 and datepart(year,DateTime)=" . $anio . " ) as marzo,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=4 and datepart(year,DateTime)=" . $anio . " ) as abril,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=5 and datepart(year,DateTime)=" . $anio . " ) as mayo,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=6 and datepart(year,DateTime)=" . $anio . " ) as junio,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=7 and datepart(year,DateTime)=" . $anio . " ) as julio,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=8 and datepart(year,DateTime)=" . $anio . " ) as agosto,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=9 and datepart(year,DateTime)=" . $anio . " ) as septiembre,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=10 and datepart(year,DateTime)=" . $anio . " ) as octubre,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=11 and datepart(year,DateTime)=" . $anio . " ) as noviembre,
(select top 1 ISNULL(AVG(CAST(MAQ_1ATC15K_VEL_INSTANT_M1ATC15K as decimal(18,3))),0)
	from tabla_TC15KM1A where datepart(month,DateTime)=12 and datepart(year,DateTime)=" . $anio . " ) as diciembre
from tabla_TC15KM1A ";
		//echo $sql;
try{
	$variable;
	$result = $this->db->Execute($sql);
	while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
		$variable = $row[0] .",". $row[1] .",". $row[2] .",". $row[3] .",". $row[4] .",". $row[5] .",". $row[6] .",". $row[7] .",". $row[8] .",". $row[9] .",". $row[10] .",". $row[11];

		break;
	}

}
catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
	throw new Exception($ex->getMessage());
}
return $variable;		
}

/*============== INICIO FUNCIONES PARA CONSULTAS DINAMICAS =====================*/
function getVelocidadPorHora($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}



	$sql = "SELECT   
	ISNULL(AVG(CAST(". $tabla ."." . $campo ." as int)),0),
	DATEPART(HH,DateTime) as horas          
	FROM      ". $tabla ."

	WHERE DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'

	GROUP BY  DATEPART(HH,DateTime)
	ORDER BY DATEPART(HH,DateTime)";	
//ChromePhp::log($sql);
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;		
}

function getVelocidadPorDias($fecha1, $fecha2, $LineaMaquinaTubo){
$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}


	$sql = "SELECT   
	ISNULL(AVG(CAST(". $tabla ."." . $campo ." as int)),0),
	DAY(DateTime) as dia,
	month(DateTime) as mes          
	FROM      ". $tabla ."

	WHERE DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'

	GROUP BY  DAY(DateTime),
	month(DateTime)
	ORDER BY  month(DateTime),
	DAY(DateTime) ASC";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getVelocidadPorMeses($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}

	$sql = "SELECT   
	ISNULL(AVG(CAST(". $tabla ."." . $campo ." as int)),0),          
	month(DateTime) as mes,
	YEAR(DateTime) as anio
	FROM      ". $tabla ."

	WHERE DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'

	GROUP BY	month(DateTime),
	YEAR(DateTime)
	ORDER BY	YEAR(DateTime)ASC,
	month(DateTime)
	";		 
	////ChromePhp::log($sql);
	try{		
		$result= $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage() . "<br>" .$sql);
	}
	return $result;
}

function getVelocidadPorAnio($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}

	$sql = "SELECT   
	ISNULL(AVG(CAST(". $tabla ."." . $campo ." as int)),0),          
	YEAR(DateTime)
	FROM      ". $tabla ."

	WHERE DateTime between '" . $fecha1 . "' and '" . $fecha2 . "'

	GROUP BY    YEAR(DateTime)
	ORDER BY	YEAR(DateTime)ASC
	";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

/* ************************* UNIDADES PRODUCIDAS ***************************************/
function getUnidadesProducidasPorHora($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(HH,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia
	";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

	function getUnidadesProducidasPorDias($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(DD,t1.DateTime) as dia , DATEPART(MM,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes
	";
ChromePhp::log($sql);
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;

	}

	function getUnidadesProducidasPorMeses($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(MM,t1.DateTime) as dia , DATEPART(YY,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes
	";		 
//ChromePhp::log($sql);
	try{		
		$result= $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage() . "<br>" .$sql);
	}
	return $result;
}

function getUnidadesProducidasPorAnio($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(YY,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia";
//ChromePhp::log($sql);
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

/* *********************** FIN UNIDADES PRODUCIDAS *************************************/


/* *********************** INICIO DE  UNIDADES DESPERDICIO *************************************/
function getUnidadesDesperdicioPorHora($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(HH,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getUnidadesDesperdicioPorDias($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(DD,t1.DateTime) as dia , DATEPART(MM,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}


function getUnidadesDesperdicioPorMeses($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(MM,t1.DateTime) as dia , DATEPART(YY,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes";
//ChromePhp::log($sql);
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}


function getUnidadesDesperdicioPorAnio($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(YY,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

/* *********************** FIN DE  UNIDADES DESPERDICIO *************************************/





//--------------------------------------------------------------------------------
/************************ INICIO DE PAROS ***************************************/
//--------------------------------------------------------------------------------
function getParosPorHora($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(HH,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getParosPorDias($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
		if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}


	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(DD,t1.DateTime) as dia , DATEPART(MM,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getParosPorMeses($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
		if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}


	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia, T2.mes from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(MM,t1.DateTime) as dia , DATEPART(YY,t1.DateTime) as mes  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia, T2.mes
order by T2.mes";
		//ChromePhp::log($sql);
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getParosPorAnio($fecha1, $fecha2, $LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
		if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}


	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  , T2.dia from (
select convert(int,t1." . $campo .") - 
convert(int,(select top 1  " . $campo ."
from ". $tabla ." where DateTime<t1.DateTime 
order by DateTime desc)) suma,DATEPART(YY,t1.DateTime) as dia  
from ". $tabla ." t1
where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
group by T2.dia
order by T2.dia";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

//-------------------------------------------------------------------------------
/* *********************** FIN DE PAROS ****************************************/
//-------------------------------------------------------------------------------



/*============== FIN FUNCIONES PARA CONSULTAS DINAMICAS =====================*/

/*============== FUNCIONES PARA CONSULTAS TIEMPO REAL =====================*/

function getEstadisticasT($LineaMaquinaTubo){

$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";
		$campo2="MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";
		$campo3="MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";
		$campo4="MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";
		$campo5="MAQ_1ATC15K_TIEMPO_MUERTO_SEG_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";
		$campo2="MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";
		$campo3="MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";
		$campo4="MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";
		$campo5="MAQ_1BTC15K_TIEMPO_MUERTO_SEG_M1BTC15K";
	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";
		$campo2="MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";
		$campo3="MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";
		$campo4="MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";
		$campo5="MAQ_2ATC15K_TIEMPO_MUERTO_SEG_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";
		$campo2="MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";
		$campo3="MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";
		$campo4="MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";
		$campo5="MAQ_2BTC15K_TIEMPO_MUERTO_SEG_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";
		$campo2="MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";
		$campo3="MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";
		$campo4="MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";
		$campo5="MAQ_3ATC15K_TIEMPO_MUERTO_SEG_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";
		$campo2="MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";
		$campo3="MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";
		$campo4="MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";
		$campo5="MAQ_3BTC15K_TIEMPO_MUERTO_SEG_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";
		$campo2="MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";
		$campo3="MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";
		$campo4="MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";
		$campo5="MAQ_4ATC15K_TIEMPO_MUERTO_SEG_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";
		$campo2="MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";
		$campo3="MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";
		$campo4="MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";
		$campo5="MAQ_4BTC15K_TIEMPO_MUERTO_SEG_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";
		$campo2="MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";
		$campo3="MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";
		$campo4="MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";
		$campo5="MAQ_5ATC15K_TIEMPO_MUERTO_SEG_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
		$campo2="MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
		$campo3="MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
		$campo4="MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
		$campo5="MAQ_5BTC15K_TIEMPO_MUERTO_SEG_M5BTC15K";
	}else {
		return null;
	}




		$sql="select ISNULL(AVG(CAST(".$tabla.".".$campo." as int)),0) as velPromedio,

(select top 1 cast(".$tabla.".".$campo2." as int) from ".$tabla."
order by cast(".$tabla.".".$campo2." as int) desc ) as UnidadesProducidas,

(select top 1 cast(".$tabla.".".$campo3." as int)
from ".$tabla." 
order by cast(".$tabla.".".$campo3." as int) desc) as UnidadesDesperdicio,

(select top 1 ISNULL(CAST(".$tabla.".".$campo4." as int),0) 
from ".$tabla." 
order by cast(".$tabla.".".$campo4." as int) desc ) as NumeroParos,

ISNULL(SUM(CAST(".$tabla.".".$campo5." as int)),0) as TMuertoSeg,

ISNULL(SUM(CAST(".$tabla.".".$campo5." as int)),0) as TMuertoMin

from ".$tabla;

try{
	$result = $this->db->Execute($sql);
}
catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
	throw new Exception($ex->getMessage());
}
return $result;

}

/////VELOCIDAD TIEMPO REAL

function getVelocidadTreal($LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_VEL_INSTANT_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_VEL_INSTANT_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_VEL_INSTANT_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_VEL_INSTANT_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_VEL_INSTANT_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_VEL_INSTANT_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_VEL_INSTANT_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_VEL_INSTANT_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_VEL_INSTANT_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_VEL_INSTANT_M5BTC15K";
	}else {
		return null;
	}



	$sql = "SELECT   
	TOP 8 ISNULL(CAST(".$tabla.".".$campo." as int),0),
	CONVERT(VARCHAR,datetime,108) as fecha       
	FROM      ".$tabla."
	order by DateTime desc";	

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;		
}


//UNIDADES PRODUCIDAS TIEMPO REAL



function getUnidadesProducidasTreal($LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "SELECT TOP 8 ISNULL(CAST(".$tabla.".".$campo." as int),0) AS bolsas, 
    			CONVERT(VARCHAR,datetime,108) as fecha
      		FROM ".$tabla."
      		order by DateTime desc";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}




//UNIDADES DE DESPERDICIO
function getUnidadesDesperdicioTreal($LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "SELECT 	top 8 ISNULL(CAST(".$tabla.".".$campo." as int),0) AS bolsas, 
    			CONVERT(VARCHAR,datetime,108) as fecha
      		FROM ".$tabla."
				order by DateTime desc";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}	

////CANTIDAD DE PAROS
function getParosTreal($LineaMaquinaTubo){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}

	$sql = "SELECT top 8	ISNULL(CAST(".$tabla.".".$campo." as int),0) AS bolsas, 
    			CONVERT(VARCHAR,datetime,108) as fecha
      		FROM ".$tabla."
      		order by DateTime desc";

	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

function getGraficaUnidadesProducidasJornada($LineaMaquinaTubo, $jornada){ 
	//jornada = 1;
	//jornada = 2;	
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_LLENAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_LLENAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_LLENAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_LLENAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_LLENAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_LLENAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_LLENAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_LLENAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_LLENAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_LLENAS_M5BTC15K";
	}else {
		return null;
	}

		$arreglo;
		$valorAnterior = 0;
		$valorActual = 0;
		$total = 0;

		//17:00 a 18:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorAnterior = (int)$row[0];
					//ChromePhp::log("valor1: " . $valorAnterior);
				}catch(Exception $err){
					$valorAnterior = 0;	
				}
				break;
			}
			if ($valorAnterior == null) {
				$valorAnterior = 0;
			}
			//ChromePhp::log("valor2: " . $valorAnterior);
			// $arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//18:00 a 19:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 07:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
					//ChromePhp::log("valor3: " . $valorAnterior);					
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}			
			//ChromePhp::log("valor4: " . $valorAnterior);
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//19:00 a 20:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000";
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 07:00:00.000";
			$limiteSuperior = date('Y-m-d') . " 08:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0 ) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//20:00 a 21:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 09:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//21:00 a 22:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 10:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;

		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//22:00 a 23:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 11:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//23:00 a 23:59 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:59:59.000";
		}else{
			$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 12:00:00.000";
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//00:00 a 01:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 00:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 01:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 13:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//01:00 a 02:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 01:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 02:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 14:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//02:00 a 03:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 02:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 03:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 15:00:00.000";
		 } 
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//03:00 a 04:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 03:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 04:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 16:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//04:00 a 05:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 04:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 05:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 17:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//05:00 a 06:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 18:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total;			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}
		//ChromePhp::log("ARREGLO: " . $arreglo);
		return $arreglo;
}

function getGraficaUnidadesDesperdicioJornada($LineaMaquinaTubo, $jornada){ 
	//jornada = 1;
	//jornada = 2;	
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_BOLSAS_VACIAS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_BOLSAS_VACIAS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_BOLSAS_VACIAS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_BOLSAS_VACIAS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_BOLSAS_VACIAS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_BOLSAS_VACIAS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_BOLSAS_VACIAS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_BOLSAS_VACIAS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_BOLSAS_VACIAS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_BOLSAS_VACIAS_M5BTC15K";
	}else {
		return null;
	}


		$arreglo;
		$valorAnterior = 0;
		$valorActual = 0;
		$total = 0;

		//17:00 a 18:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorAnterior = (int)$row[0];
					//ChromePhp::log("valor1: " . $valorAnterior);
				}catch(Exception $err){
					$valorAnterior = 0;	
				}
				break;
			}
			if ($valorAnterior == null) {
				$valorAnterior = 0;
			}
			//ChromePhp::log("valor2: " . $valorAnterior);
			// $arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//18:00 a 19:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 07:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
					//ChromePhp::log("valor3: " . $valorAnterior);					
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}			
			//ChromePhp::log("valor4: " . $valorAnterior);
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//19:00 a 20:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000";
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 07:00:00.000";
			$limiteSuperior = date('Y-m-d') . " 08:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0 ) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//20:00 a 21:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 09:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//21:00 a 22:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 10:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;

		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//22:00 a 23:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 11:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//23:00 a 23:59 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:59:59.000";
		}else{
			$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 12:00:00.000";
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//00:00 a 01:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 00:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 01:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 13:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//01:00 a 02:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 01:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 02:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 14:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//02:00 a 03:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 02:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 03:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 15:00:00.000";
		 } 
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//03:00 a 04:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 03:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 04:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 16:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//04:00 a 05:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 04:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 05:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 17:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//05:00 a 06:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 18:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log("RASPUTIN" . $sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total;			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}
		//ChromePhp::log("ARREGLO: " . $arreglo);
		return $arreglo;
}

function getGraficaCantidadParosJornada($LineaMaquinaTubo, $jornada){ 
	//jornada = 1;
	//jornada = 2;	
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_CONTADOR_DE_PAROS_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_CONTADOR_DE_PAROS_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_CONTADOR_DE_PAROS_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_CONTADOR_DE_PAROS_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_CONTADOR_DE_PAROS_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_CONTADOR_DE_PAROS_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_CONTADOR_DE_PAROS_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_CONTADOR_DE_PAROS_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_CONTADOR_DE_PAROS_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_CONTADOR_DE_PAROS_M5BTC15K";
	}else {
		return null;
	}


		$arreglo;
		$valorAnterior = 0;
		$valorActual = 0;
		$total = 0;

		//17:00 a 18:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorAnterior = (int)$row[0];
					//ChromePhp::log("valor1: " . $valorAnterior);
				}catch(Exception $err){
					$valorAnterior = 0;	
				}
				break;
			}
			if ($valorAnterior == null) {
				$valorAnterior = 0;
			}
			//ChromePhp::log("valor2: " . $valorAnterior);
			// $arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//18:00 a 19:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 07:00:00.000"; 
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
		//ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
					//ChromePhp::log("valor3: " . $valorAnterior);					
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}			
			//ChromePhp::log("valor4: " . $valorAnterior);
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//19:00 a 20:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000";
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 07:00:00.000";
			$limiteSuperior = date('Y-m-d') . " 08:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0 ) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//20:00 a 21:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 09:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//21:00 a 22:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 10:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;

		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//22:00 a 23:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 11:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}

			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//23:00 a 23:59 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:59:59.000";
		}else{
			$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 12:00:00.000";
		}
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//00:00 a 01:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 00:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 01:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 13:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//01:00 a 02:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 01:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 02:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 14:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//02:00 a 03:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 02:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 03:00:00.000";
		 }else{
		 	$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 15:00:00.000";
		 } 
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//03:00 a 04:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 03:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 04:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 16:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//04:00 a 05:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 04:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 05:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 17:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total . ", ";
			$valorAnterior = $valorActual;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//05:00 a 06:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 18:00:00.000";
		 }
		
		$sql="
		select top 1 CAST(p." . $campo . " as int)
		from " . $tabla . " p	
		WHERE p.DateTime between '" . $limiteInferior . "' and '" . $limiteSuperior . "'
		order by p.DateTime desc";
	
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				try{
					$valorActual = (int)$row[0];
				}catch(Exception $err){
					$valorActual = 0;	
				}
				break;
			}
			if ($valorActual == null) {
				$valorActual = 0;
			}
			$total = $valorActual - $valorAnterior;
			if ($total < 0) {
				$total = 0;
			}
			$arreglo = $arreglo . $total;			
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}
		//ChromePhp::log("ARREGLO: " . $arreglo);
		return $arreglo;
}


//======================================FUNCIONES PARA MINUTOS POR PAROS ==================================
function getMinutosParos($fecha1, $fecha2, $LineaMaquinaTubo, $organizacion){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_TIEMPO_MUERTO_MIN_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_TIEMPO_MUERTO_MIN_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_TIEMPO_MUERTO_MIN_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_TIEMPO_MUERTO_MIN_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_TIEMPO_MUERTO_MIN_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_TIEMPO_MUERTO_MIN_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_TIEMPO_MUERTO_MIN_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_TIEMPO_MUERTO_MIN_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_TIEMPO_MUERTO_MIN_M5BTC15K";
	}else {
		return null;
	}

	$parametro = "HH";
	if ($organizacion == "horas") {
		$parametro = "HH";
	}else if($organizacion == "dias"){
		$parametro = "DD";
	}else if($organizacion == "meses"){
		$parametro = "MM";
	}else{
		$parametro = "YY";
	}

	$sql = "select sumas= CASE when SUM(T2.suma)<0 then 0 else SUM(T2.suma) end  from (
			select convert(int,t1." . $campo .") - 
			convert(int,(select top 1 " . $campo ."
			from ". $tabla ." where DateTime<t1.DateTime 
			order by DateTime desc)) suma,DATEPART(" . $parametro . ",t1.DateTime) as hora 
			from ". $tabla ." t1
			where DateTime between '" . $fecha1 . "' and '" . $fecha2 . "') T2
			group by T2.hora
			order by T2.hora";
			
	try{
		$result = $this->db->Execute($sql);
	}
	catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
		throw new Exception($ex->getMessage());
	}
	return $result;
}

//---------------------------------------------------------------------------------------------------------


//============================= FUNCION PARA MINUTOS POR PARO, DE CADA JORNADA =============================

function getMinutosParoJornada($LineaMaquinaTubo, $jornada){
	$tabla = "";
	$campo = "";
	if ($LineaMaquinaTubo == "L1_1A") {
		//Linea 1, Maquina 1, Tubo A
		$tabla = "tabla_TC15KM1A";
		$campo = "MAQ_1ATC15K_TIEMPO_MUERTO_MIN_M1ATC15K";

	}else if ($LineaMaquinaTubo == "L1_1B") {
		//Linea 1, Maquina 1, Tubo B
		$tabla = "tabla_TC15KM1B";
		$campo = "MAQ_1BTC15K_TIEMPO_MUERTO_MIN_M1BTC15K";

	}else if ($LineaMaquinaTubo == "L1_2A") {
		//Linea 1, Maquina 2, Tubo A
		$tabla = "tabla_TC15KM2A";
		$campo = "MAQ_2ATC15K_TIEMPO_MUERTO_MIN_M2ATC15K";

	}else if ($LineaMaquinaTubo == "L1_2B") {
		//Linea 1, Maquina 2, Tubo B
		$tabla = "tabla_TC15KM2B";
		$campo = "MAQ_2BTC15K_TIEMPO_MUERTO_MIN_M2BTC15K";

	}else if ($LineaMaquinaTubo == "L1_3A") {
		//Linea 1, Maquina 3, Tubo A
		$tabla = "tabla_TC15KM3A";
		$campo = "MAQ_3ATC15K_TIEMPO_MUERTO_MIN_M3ATC15K";

	}else if ($LineaMaquinaTubo == "L1_3B") {
		//Linea 1, Maquina 3, Tubo B
		$tabla = "tabla_TC15KM3B";
		$campo = "MAQ_3BTC15K_TIEMPO_MUERTO_MIN_M3BTC15K";

	}else if ($LineaMaquinaTubo == "L1_4A") {
		//Linea 1, Maquina 4, Tubo A
		$tabla = "tabla_TC15KM4A";
		$campo = "MAQ_4ATC15K_TIEMPO_MUERTO_MIN_M4ATC15K";

	}else if ($LineaMaquinaTubo == "L1_4B") {
		//Linea 1, Maquina 4, Tubo B
		$tabla = "tabla_TC15KM4B";
		$campo = "MAQ_4BTC15K_TIEMPO_MUERTO_MIN_M4BTC15K";

	}else if ($LineaMaquinaTubo == "L1_5A") {
		//Linea 1, Maquina 5, Tubo A
		$tabla = "tabla_TC15KM5A";
		$campo = "MAQ_5ATC15K_TIEMPO_MUERTO_MIN_M5ATC15K";

	}else if ($LineaMaquinaTubo == "L1_5B") {
		//Linea 1, Maquina 5, Tubo B
		$tabla = "tabla_TC15KM5B";
		$campo = "MAQ_5BTC15K_TIEMPO_MUERTO_MIN_M5BTC15K";
	}else {
		return null;
	}

		$Hora = null;
		$arreglo;
		//18:00 a 19:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 18:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 06:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 07:00:00.000";
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
		ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//19:00 a 20:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 19:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 07:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 08:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//20:00 a 21:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 20:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 08:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 09:00:00.000";
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//21:00 a 22:00 del dia anterior
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 21:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 09:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 10:00:00.000";
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//22:00 a 23:00 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 22:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 10:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 11:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//23:00 a 23:59 del dia anterior
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d',strtotime("-1 days")) . " 23:00:00.000"; 		
			$limiteSuperior = date('Y-m-d',strtotime("-1 days")) . " 23:59:59.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 11:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 12:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//00:00 a 01:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 00:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 01:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 12:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 13:00:00.000";
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}


		//01:00 a 02:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 01:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 02:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 13:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 14:00:00.000";	
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//02:00 a 03:00 del dia actual
		if ($jornada == 1) {
		 	$limiteInferior = date('Y-m-d') . " 02:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 03:00:00.000";
		 } else{
		 	$limiteInferior = date('Y-m-d') . " 14:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 15:00:00.000";
		 }
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//03:00 a 04:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 03:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 04:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 15:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 16:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//04:00 a 05:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 04:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 05:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 16:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 17:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora . ", ";
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		//05:00 a 06:00 del dia actual
		if ($jornada == 1) {
			$limiteInferior = date('Y-m-d') . " 05:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 06:00:00.000"; 
		}else{
			$limiteInferior = date('Y-m-d') . " 17:00:00.000"; 		
			$limiteSuperior = date('Y-m-d') . " 18:00:00.000"; 
		}
		
		$sql = "SELECT (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . "   p
	where p.DateTime <= '" . $limiteSuperior . "' and p.DateTime >= '" . $limiteInferior . "'
	order by p.Datetime desc),0)) - (SELECT ISNULL((SELECT top 1 ISNULL(CAST(p." . $campo . " as int),0)
	from " . $tabla . " p
	where p.DateTime >= '" . $limiteInferior . "' and p.DateTime <= '" . $limiteSuperior . "'),0))";
ChromePhp::log($sql);
		try{
			$result = $this->db->Execute($sql);
			while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) {
				$Hora = $row[0];
				try {
					$aux = (int)$Hora;
					if ($aux < 0) {
						$Hora = "0";
					}
				} catch (Exception $e) {
					$Hora = "0";
				}
				break;
			}
			if ($Hora == null) {
				$Hora = "0";
			}
			$arreglo = $arreglo . $Hora;
		}
		catch(Exception $ex){
			// si existe un error se deshace la transacci&#65533;n
			throw new Exception($ex->getMessage());
			return;
		}

		ChromePhp::log($arreglo);
		return $arreglo;
}
//---------------------------------------------------------------------------------------------------------

}

?>